package com.rambo.sdh.controller;

import java.util.Date;
import java.util.List;

import com.rambo.sdh.common.controller.BaseController;
import com.rambo.sdh.dao.pojo.RolePO;
import com.rambo.sdh.dao.pojo.UserPO;
import com.rambo.sdh.service.inter.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserAction extends BaseController {

	@Autowired
	private UserService userService;

	@RequestMapping("/selectList")
	public String list() {

		List<UserPO> userPOs = userService.getUserList();
		System.out.println(userPOs);
		return "success";

	}

	@RequestMapping("/add")
	public String add() {

		UserPO user = new UserPO();

		user.setBirthday(new Date());
		user.setEmail("aa");
		user.setName("bb");
		user.setPasswd("000");
		user.setPhone("123");
		user.setSex("nv");
		user.setSorts("111");
		user.setYxbz("nnn");
		user.setPhoto("mmmm");

		RolePO role = new RolePO();

		role.setCname("vvv");
		role.setSorts("sdsd");
		role.setType("aa");
		role.setYxbz("sdasdasd");

		user.getRolePOs().add(role);
		userService.addUser(user);
		return "success";
	}

	@RequestMapping("/delete")
	public String delete() {

		userService.deleteUserById(8);

		return "success";

	}

	@RequestMapping("/update")
	public String update() {

		UserPO user = new UserPO();
		user.setId(6);
		user.setBirthday(new Date());
		user.setEmail("dfgdfgdfgdfg");
		user.setName("dfgdfgdfg");
		user.setPasswd("0011110");
		user.setPhone("123aaaa");
		user.setSex("nvadasdasd");
		user.setSorts("111asd");
		user.setYxbz("nnnasd");
		user.setPhoto("mmmmasdd");
		userService.updateUser(user);

		return "success";

	}

	// 通过user得到role
	@RequestMapping("/getRoleByid")
	public String getRoleByid(@RequestParam("id") Integer id) {

	List<RolePO> rolePOs=userService.getRolePOByUserId(id);
	System.out.println(rolePOs);
		return "success";
	}

	// 通过role得到user
	@RequestMapping("/getUserByid")
	public String getUserByid(@RequestParam("id") Integer id) {

	List<UserPO> userPOs=userService.getUserPOByRoleId(id);
	System.out.println(userPOs);
		return "success";
	}

}
